package summerprojectFX.controller;

import java.io.File;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import javafx.stage.FileChooser;
import javafx.stage.PopupWindow;
import javafx.stage.Stage;

import summerprojectFX.model.Song;
import summerprojectFX.parser.DOCFile;
import summerprojectFX.parser.DOCXFile;
import summerprojectFX.parser.PDFFile;
import summerprojectFX.parser.TextFile;
import summerprojectFX.view.MainView;

public class OpenFile {

	private File file;

	public OpenFile(MainView view, Stage primary) {

		FileChooser fc = new FileChooser();
		FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
				"Supported files (*.txt, *.doc, *.docx, *.pdf)", "*.txt",
				"*.doc", "*.docx", "*.pdf");
		fc.getExtensionFilters().add(extFilter);

		file = fc.showOpenDialog(primary);

		if (file != null)
			handleOpenFile(view);

	}

	private void handleOpenFile(final MainView view) {
		Song song = new Song();
		System.out.println("Opening: " + file.getAbsolutePath() + ".\n");
		String[] fileType = file.getAbsolutePath().split("\\.");
		if (fileType[1].equals("docx")) {
			DOCXFile docx = new DOCXFile(file);
			song = docx.read();
			view.addSong(song);

		} else if (fileType[1].equals("doc")) {
			DOCFile doc = new DOCFile(file);
			song = doc.read();
			view.addSong(song);
		} else if (fileType[1].equals("pdf")) {
			PDFFile pdf = new PDFFile(file);
			song = pdf.read();
			view.addSong(song);
		} else if (fileType[1].equals("txt")) {
			TextFile tfr = new TextFile(file);
			song = tfr.read();
			view.addSong(song);
		}
                
                if(view.getCheckedOpen()== false){
                final Stage dialogStage = new Stage();
                                            dialogStage.setTitle("Set Key");
                                            Group root = new Group();
                                            Scene scene = new Scene(root, 270,
                                           120 );
                                            Label key = new Label("Make sure to set the key of your song\n");
                                            Label choice = new Label("Dont show this message again");
                                            final CheckBox check = new CheckBox();
                                            VBox vb = new VBox();
                                            Button ok = new Button("Ok");
                                            ok.setOnAction(new EventHandler<ActionEvent>() {
                                                    public void handle(final ActionEvent event) {
                                                            {
                                                                if(check.isSelected()){
                                                                   view.setCheckedOpen(true);
                                                                }
                                                              dialogStage.close();
                                                                    
                                                                
                                                            }
                                                    }
                                            });
                                            vb.setAlignment(Pos.CENTER);
                                            HBox hb = new HBox();
                                            hb.getChildren().addAll(check, choice);
                                            vb.getChildren().addAll(key, hb, ok );
                                            vb.setSpacing(10);
                                            vb.setStyle("-fx-padding: 20 0 0 10;");
                                            hb.setAlignment(Pos.CENTER);
                                            root.getChildren().add(vb);
                                           
                                            dialogStage.setScene(scene);
                                            dialogStage.show();
                }
                
	}

}
